package com.briup.service.impl;

import com.briup.bean.CallPer;
import com.briup.bean.CallLog;
import com.briup.bean.Consume;
import com.briup.bean.Plan;
import com.briup.dao.CallLogMapper;
import com.briup.dao.PlanMapper;
import com.briup.service.ICalllogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * 通话日志服务对象
 */
@Service
public class CalllogServiceImpl implements ICalllogService {
    @Autowired
    private CallLogMapper calllogMapper;
    @Autowired
    private PlanMapper planMapper;


    /**
     * 查询用户指定时间的通话统计信息
     * @param tel
     * @param callTime
     * @return
     */
    @Override
    public List<CallLog> queryMonthDatas(String tel, String callTime) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("tel", tel);
        if (callTime.length() > 4 ) {
            callTime = callTime.substring(0, 4);
        }
        paramMap.put("year", callTime);
        List<CallLog> list = calllogMapper.queryMonthDatas(paramMap);
//        list.forEach(System.out::println);
        return list;
    }

    @Override
    public CallLog queryCurrDatas(String tel, String calltime) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("tel", tel);
        String[] split = calltime.split("[-]");
        int month = Integer.parseInt(split[1]);
        paramMap.put("year",split[0]);
        paramMap.put("month",month);
        CallLog calllog = calllogMapper.queryCurrDatas(paramMap);
        return calllog;
    }

    @Override
    public CallLog queryPreDatas(String tel, String calltime) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("tel", tel);
        String[] split = calltime.split("[-]");
//        System.out.println("123:"+split[0]+"-"+split[1]);
        int month = Integer.parseInt(split[1])-1;
        int year = Integer.parseInt(split[0]);
        if(month==0) {
            month=12;
            year=year-1;
        }
        paramMap.put("year",year);
        paramMap.put("month",month);
        CallLog calllog = calllogMapper.queryCurrDatas(paramMap);
        return calllog;
    }

    @Override
    public Plan queryCurrRemainder(String tel, String calltime) {
        CallLog calllog = queryCurrDatas(tel, calltime);
        System.out.println("calllog = " + calllog);
        Plan plan = planMapper.findByCurr(calllog.getOPlan());
        int consume = 0;
        double call_consume = 0.0;
        double flux_consume = 0.0;
        double message_consume = 0.0;

        System.out.println("plan = " + plan);
        int call_minu = calllog.getSumDuration() - Integer.parseInt(plan.getCallDuration().split("分")[0]);
        double flux = 0.0;
        if(plan.getFlux().contains("M")) {
            flux = Double.parseDouble(calllog.getFluxNum())/1000-Double.parseDouble(plan.getFlux().split("M")[0])/1000;
        }else {
            flux = Double.parseDouble(calllog.getFluxNum())/1000-Double.parseDouble(plan.getFlux().split("G")[0]);
        }
        if(call_minu>0)
            call_consume = Double.parseDouble(plan.getCallTariff().split("元")[0])*call_minu;
        if(flux>0)
            flux_consume = Double.parseDouble(plan.getFluxTariff().split("元")[0])*flux;
        message_consume = Double.parseDouble(plan.getMessageTariff().split("元")[0])*calllog.getSumMessages();
        consume = (int) (plan.getTariff()+call_consume+flux_consume+message_consume);
        plan.setId(consume);
        int call_num = Integer.parseInt(plan.getCallDuration().substring(0,2))-calllog.getSumDuration();
        double flux_num = 0.0;
        String dw = plan.getFlux().substring(plan.getFlux().length()-2);
        if(dw.equals("MB")) {
            flux_num = Integer.parseInt(plan.getFlux().substring(0,plan.getFlux().length()-2))
                    -Double.parseDouble(calllog.getFluxNum());
        }else{
            flux_num = Integer.parseInt(plan.getFlux().substring(0,plan.getFlux().length()-2))*1000
                    -Double.parseDouble(calllog.getFluxNum());
        }
        if(flux_num<=0.0) {
            plan.setFlux("0MB");
        }else if(flux_num>=1000){
            flux_num = (double)(Math.round(flux_num/1000*100)/100);
            plan.setFlux(flux_num+"GB");
        }else{
            plan.setFlux(flux_num+"MB");
        }
        if(call_num<=0) {
            plan.setCallDuration("0分钟");
        }else {
            plan.setCallDuration(call_num+"分钟");
        }
        plan.setMessageTariff("0条");

        return plan;
    }

    @Override
    public List<Plan> queryMonthPlans(String tel, String calltime) {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("tel", tel);
        if (calltime.length() > 4 ) {
            calltime = calltime.substring(0, 4);
        }
        paramMap.put("year", calltime);
        List<Plan> list = planMapper.queryMonthPlans(paramMap);
//        list.forEach(System.out::println);
        return list;
    }

    @Override
    public List<Consume> queryMonthConsume(String tel, String year) {
        List<CallLog> calllogs = queryMonthDatas(tel, year);
        List<Plan> plans = queryMonthPlans(tel, year);
        List<Consume> list = new ArrayList<>();
        for(int i=0;i<calllogs.size();i++) {
            int consume = 0;
            double call_consume = 0.0;
            double flux_consume = 0.0;
            double message_consume = 0.0;
            Consume con = new Consume();
            int call_minu = calllogs.get(i).getSumDuration() - Integer.parseInt(plans.get(i).getCallDuration().split("分")[0]);
            double flux = 0.0;
            if(plans.get(i).getFlux().contains("M")) {
                flux = Double.parseDouble(calllogs.get(i).getFluxNum())/1000-Double.parseDouble(plans.get(i).getFlux().split("M")[0])/1000;
            }else {
                flux = Double.parseDouble(calllogs.get(i).getFluxNum())/1000-Double.parseDouble(plans.get(i).getFlux().split("G")[0]);
            }
            if(call_minu>0)
                call_consume = Double.parseDouble(plans.get(i).getCallTariff().split("元")[0])*call_minu;
            if(flux>0)
                flux_consume = Double.parseDouble(plans.get(i).getFluxTariff().split("元")[0])*flux;
            message_consume = Double.parseDouble(plans.get(i).getMessageTariff().split("元")[0])*calllogs.get(i).getSumMessages();
            consume = (int) (plans.get(i).getTariff()+call_consume+flux_consume+message_consume);
//            System.out.println(consume);
            con.setNum(consume);
            con.setFlux((int) flux_consume);
            con.setCall((int) call_consume);
            con.setMessage((int) message_consume);
            list.add(con);
        }
        return list;
    }

    @Override
    public CallPer queryPerRemainder(String tel, String calltime) {
        CallLog curr = queryCurrDatas(tel, calltime);
        CallLog per = queryPreDatas(tel, calltime);
        CallPer callPer = new CallPer();
        callPer.setSumDuration(curr.getSumDuration());
        callPer.setFluxNum(curr.getFluxNum());
        callPer.setSumMessages(curr.getSumMessages());
        DecimalFormat df = new DecimalFormat("#.00");
        System.out.println(curr);
        System.out.println(per);
        double durationPer = (double)curr.getSumDuration()/per.getSumDuration()-1;
        if(per.getSumMessages()==0) per.setSumMessages(1);
        double messagesPer = (double)curr.getSumMessages()/per.getSumMessages()-1;
        if(per.getFluxNum().equals("0")) per.setFluxNum("1");
        double fluxPer = Double.parseDouble(curr.getFluxNum())/Double.parseDouble(per.getFluxNum())-1;
        callPer.setDurationPer(Double.parseDouble(df.format(durationPer*100)));
        callPer.setMessagesPer(Double.parseDouble(df.format(messagesPer*100)));
        callPer.setFluxPer(Double.parseDouble(df.format(fluxPer*100)));
        System.out.println(callPer);
        return callPer;
    }

    @Override
    public Consume queryYearConsume(String tel, String calltime) {
        List<Consume> list = queryMonthConsume(tel, calltime);
        int sum = 0;
        double flux_sum = 0.0;
        double message_sum = 0.0;
        double call_sum = 0.0;
        for(int i=0;i<list.size();i++) {
            sum = sum+list.get(i).getNum();
            flux_sum = flux_sum+list.get(i).getFlux();
            message_sum = message_sum+list.get(i).getMessage();
            call_sum = call_sum+list.get(i).getCall();
        }
        Consume con = new Consume(sum,(int)(Math.random()*30), (int)(Math.random()*20),(int)call_sum*100/sum);
        System.out.println(con);
        return con;
    }


}
